// the state
#include "tbox.h"

static tb_atomic32_t  g_tbox_state = TB_STATE_END;

int tb_init()
{
    // printf("g_state 10:%d\n",g_state);

    // if (TB_STATE_OK == tb_atomic32_fetch_and_cmpset(&g_state, TB_STATE_END, TB_STATE_OK)){
    //     printf("g_state 12:%d\n",g_state);
    //     return tb_true;
    // }
    //     return tb_true;
    // printf("g_state 20:%d\n",g_state);

    // if (TB_STATE_OK == tb_atomic32_fetch_and_cmpset(&g_state, TB_STATE_END, TB_STATE_OK)){
    //     printf("g_state 21:%d\n",g_state);
    //     return tb_true;
    // }
    // printf("g_state 30:%d\n",g_state);
    // return tb_false;
    // have been inited?
    if (TB_STATE_OK == tb_atomic32_fetch_and_cmpset(&g_tbox_state, TB_STATE_END, TB_STATE_OK))
        return tb_true;

    // init trace
    if (!tb_trace_init()) return tb_false;

    // trace
    tb_trace_d("init: ..");
    if (!tb_memory_init_env(tb_null)) return tb_false;

    // trace
    tb_trace_d("init: ok");

    // ok
    return tb_true;
}

tb_void_t tb_exit()
{
    // exit memory environment
    tb_memory_exit_env();

    // trace
    tb_trace_d("exit: ok");

    // exit trace
    tb_trace_exit();

    // end
    g_tbox_state = TB_STATE_END;
    // tb_atomic32_set(&g_state, TB_STATE_END);
}